using System;
using System.Collections;
using UnityEngine;

namespace __Naptime.Steam
{
	public sealed class ResolutionUiWindowScaleChanger : MonoBehaviour
	{
		private void Awake()
		{
			ResolutionManager.OnResolutionChange += OnResolutionChange;
		}

		private void Update()
		{
		}

		private void Start()
		{
			Invoke("ChangeResolution", 0.1f);
		}

		private void OnDestroy()
		{
			ResolutionManager.OnResolutionChange -= OnResolutionChange;
		}

		private void OnResolutionChange()
		{
			ChangeResolution();
		}

		private void ChangeResolution()
		{
			string @string = NintendoPlayerPrefs.GetString("RESOLTION_KEY", ResolutionManager.DefaultResolutionKey);
			if (@string == "1280x800")
			{
				base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
			}
			else if (@string == "1680x1050")
			{
				base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
			}
			else
			{
				base.transform.localScale = Vector3.one;
			}
			StartCoroutine(ChangeSizeWithDelay(@string));
		}

		private IEnumerator ChangeSizeWithDelay(string resolutionKey)
		{
			yield return new WaitForEndOfFrame();
			yield return new WaitForEndOfFrame();
			yield return new WaitForEndOfFrame();
			_ = Camera.main.aspect;
			float num = (float)Screen.width / (float)Screen.height;
			if (Math.Abs(num - 1.6f) < 0.01f)
			{
				Handle1280X800Resolution(resolutionKey);
			}
			else if (Math.Abs(num - 1.6f) < 0.01f)
			{
				Handle1680X1050Resolution(resolutionKey);
			}
			else if (Math.Abs(num - 1.3333334f) < 0.01f)
			{
				Handle4X3Resolution(resolutionKey);
			}
			else if (resolutionKey == "1280x800")
			{
				base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
			}
			else if (resolutionKey == "1680x1050")
			{
				base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
			}
			else
			{
				base.transform.localScale = Vector3.one;
			}
		}

		private void Handle1920X1080Resolution()
		{
		}

		private void Handle1280X720Resolution(string resolutionKey)
		{
		}

		private void Handle1280X800Resolution(string resolutionKey)
		{
			if (resolutionKey == "1680x1050" || resolutionKey == "1280x800")
			{
				base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
			}
			base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
		}

		private void Handle4X3Resolution(string resolutionKey)
		{
			base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
		}

		private void Handle1366X768Resolution(string resolutionKey)
		{
		}

		private void Handle1600X900Resolution(string resolutionKey)
		{
		}

		private void Handle1680X1050Resolution(string resolutionKey)
		{
			if (resolutionKey == "1680x1050" || resolutionKey == "1280x800")
			{
				base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
			}
			base.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
		}

		private void Handle1760X990Resolution(string resolutionKey)
		{
		}

		private void Handle3440X1400Resolution(string resolutionKey)
		{
		}

		private void CheckUIFitResolution()
		{
			RectTransform[] componentsInChildren = GetComponentInParent<Canvas>().GetComponentsInChildren<RectTransform>();
			foreach (RectTransform rectTransform in componentsInChildren)
			{
				Vector3[] array = new Vector3[4];
				rectTransform.GetWorldCorners(array);
				bool flag = false;
				Vector3[] array2 = array;
				foreach (Vector3 position in array2)
				{
					Vector3 vector = Camera.main.WorldToViewportPoint(position);
					if (vector.x < 0f || vector.x > 1f || vector.y < 0f || vector.y > 1f)
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					AdjustElementScaleToFitResolution(rectTransform);
				}
			}
		}

		private void AdjustElementScaleToFitResolution(RectTransform element)
		{
			Vector2 vector = new Vector2(Screen.width, Screen.height);
			Vector2 size = element.rect.size;
			float a = vector.x / size.x;
			float b = vector.y / size.y;
			float num = Mathf.Min(a, b);
			element.localScale *= num;
			Debug.Log("Scale adjusted for UI element: " + element.name);
		}
	}
}
